package com.pxx.item.controller;

import com.pxx.item.pojo.TbCategory;
import com.pxx.item.service.TbCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.pxx.common.utils.R;

/**
 * @author : Yoo19
 * @description :
 * @createDate : 2025/6/23 14:08 08
 */
@RestController
@RequestMapping("/item/category")
public class CategoryController {

    @Autowired
   private TbCategoryService tbCategoryService;

    /**
     * 根据父id查询商品分类
     * @param pid
     * @return
     */
    @RequestMapping("list")
    public ResponseEntity<List<TbCategory>> querryCategoryListByPrentId(@RequestParam(value = "pid",defaultValue = "0")Long pid){
     List<TbCategory> list =  tbCategoryService.querryCategoryListByPrentId(pid);
     if(CollectionUtils.isEmpty(list)){
         return ResponseEntity.notFound().build();
     }
     return ResponseEntity.ok(list);
    }

    /**
     * 添加分类
     * @param tbCategory
     * @return
     */
    @PostMapping("/addCateGory")
    public R saveCategory(@RequestBody TbCategory tbCategory){
            tbCategoryService.saveCategory(tbCategory);
           return R.OK();
    }

    /**
     * 修改分类
     * @param id
     * @param name
     * @return
     */
    @RequestMapping("/updateCateGory")
    public R updateCategory(@RequestParam(value = "id",defaultValue = "0")Long id,@RequestParam(value = "name")String name){
        tbCategoryService.updateCategory(id,name);
        return R.OK();
    }

    /**
     * 删除最底层分类
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public R delete(@RequestParam(value = "id",defaultValue = "0")Long id){
        tbCategoryService.delete(id);
        return R.OK();
    }


    @GetMapping("bid/{bid}")
    public ResponseEntity<List<TbCategory>> queryCategoryListByBrandId(@PathVariable("bid")Long bid){
      List<TbCategory> list =  tbCategoryService.queryCategoryListByBrandId(bid);
      if(CollectionUtils.isEmpty(list)){
          return ResponseEntity.notFound().build();
      }

      return ResponseEntity.ok(list);
    }







    /**
     * 普通请求方式
     * @param pid
     * @param name
     * @return
     */
    @RequestMapping("/code")
    public Long querryCode(@RequestParam(value = "pid",defaultValue = "0",required = true)Long pid,
                           @RequestParam(value = "name",required = true)String name ){
        System.out.println("使用该传参方式成功运行");
        System.out.println("name=" + name);
        return pid;
    }

    /**
     * 传json格式的参数
     * @param tbCategory
     * @return
     */
    @RequestMapping("/json")
    public Map<String,Object> queryJson(@RequestBody TbCategory tbCategory){
        Map<String, Object> map = new HashMap<>();
        map.put("category",tbCategory);
        return map;
    }

    /**
     * 链路传参请求
     * @param pid
     * @param bid
     * @return
     */
    @RequestMapping("/code/{pid}/{bid}")
    public String queryCodePath(@PathVariable("pid")Long pid,@PathVariable("bid")Long bid){
        String a = String.valueOf(pid + bid);
        return a;
    }

}
